package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version: 1.0
 * @Author: 李佳毅
 * @description:
 * @date: 2025-07-06 21:34
 */

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    //新增菜品
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品");
        boolean result = dishService.saveWithFlavor(dishDTO);
        return result ? Result.success() : Result.error("新增菜品失败");
    }

    //分页查询 按照条件查询菜品信息
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result findDishByPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询");
        PageResult pageResult = dishService.findDishByPage(dishPageQueryDTO);
        return pageResult != null ? Result.success(pageResult) : Result.error("分页查询失败");
    }

    //根据多个id批量删除菜品
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteByIds(@RequestParam("ids") List<Long> ids) {
        log.info("批量删除菜品");
        return dishService.deleteByIds(ids) ? Result.success() : Result.error("批量删除菜品失败");
    }

    //根据id查询菜品
    @GetMapping("/{dishId}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> findById(@PathVariable("dishId") Long dishId) {
        log.info("根据id查询菜品");
        DishVO dishVO = dishService.findById(dishId);
        return dishVO != null ? Result.success(dishVO) : Result.error("查询菜品失败");
    }

    //修改菜品信息,包括口味
    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品信息 :{}", dishDTO);
        boolean result = dishService.updateWithFlavor(dishDTO);
        return result ? Result.success() : Result.error("修改菜品信息失败");
    }

    //修改菜品的状态
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品的状态")
    public Result startOrStop(@PathVariable("status") Integer status, Long id) {
        log.info("修改菜品的状态");
        return dishService.startOrStop(status, id) ? Result.success() : Result.error("修改菜品的状态失败");
    }

    //查询所有的菜品数据
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        log.info("根据分类id查询菜品");
        List<Dish> list = dishService.list(categoryId);
        return list != null ? Result.success(list) : Result.error("根据分类id查询菜品失败");
    }
}
